import Vue from 'vue'
import 'normalize.css'
import Router from 'vue-router'
import element from 'element-ui'
import App from './App' //  局部组件
import '../node_modules/element-ui/lib/theme-chalk/index.css'
import login from './components/login'
import reg from './components/reg'
import details from './components/details'
import aa from './components/aa'

const router = new Router({
  routes: [
    {
      path: '/login',
      name: 'login',
      component: login
    },
    {
      path: '/reg',
      name: 'reg',
      component: reg
    },
    {
      path: '/details',
      name: 'details',
      component: details,
      meta: {
        requireLogin: true
      },
      children: [
        {
          path: 'details_a',
          name: 'details_a',
          component: aa
        }
      ]
    }
  ]
})
const auth = false
/*
*  to: Route: 即将要进入的目标 路由对象
*  from: Route: 当前导航正要离开的路由
*  next: Function: 一定要调用该方法来 resolve 这个钩子。执行效果依赖 next 方法的调用参数。
* */
router.beforeEach((to, from, next) => {
  console.log(to)
  console.log(from)
  console.log(next)
  if (to.matched.some(value => value.meta.requireLogin)) {
    if (auth) {
      next()
    } else {
      next('/login')
    }
  } else {
    next()
  }
})
Vue.use(Router)
Vue.use(element)
/* eslint-disable no-new */
new Vue({
  render: h => h(App),
  router
}).$mount('#app')
